package top.lv666.graduation_design_serve.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lv666.graduation_design_serve.pojo.UserInfo;
import top.lv666.graduation_design_serve.service.UserService;
import top.lv666.graduation_design_serve.util.CacheData;
import top.lv666.graduation_design_serve.util.Tools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    public Logger logger = Logger.getLogger(UserController.class);
    public Map<String, Object> returnMap = new HashMap<>();
    private Map<String, Object> paramsMap = new HashMap<>();
    @Autowired
    private UserService userService;

    /**
     * @apiNote 取消关注
     */
    @PostMapping("/clearFocus")
    public Map<String, Object> clearFocus(
            HttpServletRequest request,
            @RequestBody Map<String, Object> map
    ) {
        returnMap.clear();

        return returnMap;
    }

    /**
     * @apiNote 我关注谁了
     */
    @GetMapping("/myFoucs")
    public Map<String, Object> myFoucs(
            HttpServletRequest request,
            @RequestParam Integer getPage,
            @RequestParam Integer getNumber
    ) {
        returnMap.clear();
        paramsMap.clear();
        try {
            paramsMap.put("request", request);
            paramsMap.put("getPage", getPage);
            paramsMap.put("getNumber", getNumber);
            returnMap.put("data", userService.selectMyFoucs(paramsMap));
            returnMap.put("msg", "查询成功！");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "查询失败【原因:】" + e.getMessage());
            returnMap.put("flag", false);
            // 写入日志
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 谁关注我了
     */
    @GetMapping("/myFans")
    public Map<String, Object> myFans(
            HttpServletRequest request,
            @RequestParam Integer getPage,
            @RequestParam Integer getNumber
    ) {
        returnMap.clear();
        paramsMap.clear();
        try {
            paramsMap.put("request", request);
            paramsMap.put("getPage", getPage);
            paramsMap.put("getNumber", getNumber);
            returnMap.put("data", userService.selectMyFans(paramsMap));
            returnMap.put("msg", "查询成功！");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "查询失败【原因:】" + e.getMessage());
            returnMap.put("flag", false);
            // 写入日志
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 我关注谁了
     */
    @GetMapping("/myFocus")
    public Map<String, Object> myFocus(
            HttpServletRequest request,
            @RequestBody Map<String, Object> map
    ) {
        returnMap.clear();
        try {
            map.put("request", request);
            userService.focusOnUserById(map);
            returnMap.put("msg", "关注成功！");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "关注失败【原因:】" + e.getMessage());
            returnMap.put("flag", false);
            // 写入日志
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 关注某个用户
     */
    @PostMapping("/focusOnUserById")
    public Map<String, Object> focusOnUserById(
            HttpServletRequest request,
            @RequestBody Map<String, Object> map
    ) {
        returnMap.clear();
        try {
            map.put("request", request);
            userService.focusOnUserById(map);
            returnMap.put("msg", "关注成功！");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "关注失败【原因:】" + e.getMessage());
            returnMap.put("flag", false);
            // 写入日志
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 获取用户的基本信息
     */
    @GetMapping("/getUserInfoById")
    public Map<String, Object> getUserInfoById(
            @RequestParam String id
    ) {
        returnMap.clear();
        try {
            returnMap.put("msg", "查询成功！");
            returnMap.put("flag", true);
            returnMap.put("data",userService.queryUserInfoById(id));
        } catch (Exception | Error e) {
            returnMap.put("msg", "登陆失败【原因:】" + e.getMessage());
            returnMap.put("flag", false);
            //写入日志
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 获取用户的基本信息
     */
    @GetMapping("/getUserInfo")
    public Map<String, Object> selectOneUser(
            HttpServletRequest request
    ) {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            UserInfo userInfo = userService.queryUserInfo(request);
            returnMap.put("msg", "查询成功！");
            returnMap.put("flag", true);
            returnMap.put("data", userInfo);
            return returnMap;
        } catch (Exception | Error e) {
            returnMap.put("msg", "登陆失败【原因:】" + e.getMessage());
            returnMap.put("flag", false);
            //写入日志
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote USER 用户账号密码登录
     */
    @PostMapping("/usePasLogin")
    public Map<String, Object> selectOneUser(
            @RequestBody Map<String, Object> map,
            HttpServletResponse response
    ) {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            map.put("response", response);
            String uid = userService.selectOneUser(map);
            returnMap.put("msg", "登陆成功！");
            returnMap.put("flag", true);
            returnMap.put("userID", uid);
        } catch (Error | Exception e) {
            returnMap.put("msg", "登陆失败【原因:】" + e.getMessage());
            returnMap.put("flag", false);
            //写入日志
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote USER 用户注册
     */
    @PostMapping("/registered")
    public Map<String, Object> createOneUser(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            if (userService.selectUserIsExits((String) map.get("userName")) && CacheData.checkCacheDate(Tools.getIpAddr(request), (String) map.get("code"))) {
                userService.createOneUser(map);
                returnMap.put("msg", "注册成功！");
                returnMap.put("flag", true);
                //删除缓存
                CacheData.deleteCacheDate(Tools.getIpAddr(request));
            } else if (!userService.selectUserIsExits((String) map.get("userName"))) {
                throw new Error("该账号已存在！");
            } else {
                throw new Error("验证码错误！");
            }
        } catch (Error | Exception e) {
            returnMap.put("msg", "【注册失败原因：】" + e.getMessage());
            returnMap.put("flag", false);
            //写入日志
            logger.info(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 手机验证码登录
     */
    @PostMapping("/phoneCodeLogin")
    public Map<String, Object> phoneLogin(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //添加请求体
        map.put("request", request);
        map.put("response", response);
        Map<String, Object> returnMap = new HashMap<>();
        try {
            String uid = userService.phoneCodeLogin(map);
            returnMap.put("msg", "登录成功！");
            returnMap.put("flag", true);
            returnMap.put("userID", uid);
        } catch (Error | Exception e) {
            returnMap.put("msg", "登录失败" + e.getMessage());
            returnMap.put("flag", false);
            logger.error("【手机登录失败】" + e.getMessage());
        }
        return returnMap;
    }

    /**
     * @apiNote 账号注销
     */
    @PostMapping("/logoutUser")
    public Map<String, Object> killerUser(
            @RequestBody Map<String, Object> map
    ) {
        Map<String, Object> returnMap = new HashMap<>();
        try {

        } catch (Exception | Error e) {
            //写入日志
            logger.error(e);
        }
        return returnMap;
    }


    /**
     * @apiNote 修改用户
     */
    @PostMapping("/editUserInfo")
    public Map<String, Object> editUser(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            map.put("request", request);
            userService.ModifyUserInfo(map);
            returnMap.put("msg", "用户信息修改成功");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "用户信息修改失败" + e.getMessage());
            returnMap.put("flag", false);
            logger.error("【修改失败】" + e.getMessage());
        }
        return returnMap;
    }


    /**
     * @apiNote 获取推荐用户
     */
    @GetMapping("/getHotUser")
    public Map<String, Object> hotUser(
            @RequestParam(defaultValue = "10") int page,
            @RequestParam(defaultValue = "1") int limit
    ) {
        returnMap.clear();
        try {
            List<Map<String, Object>> mapList = userService.getHotUser(page, limit);
            returnMap.put("msg", "高质量用户查询成功");
            returnMap.put("flag", true);
            returnMap.put("data", mapList);
        } catch (Exception | Error e) {
            returnMap.put("msg", "高质量用户查询失败" + e.getMessage());
            returnMap.put("flag", false);
            logger.error("【修改失败】" + e.getMessage());
        }
        return returnMap;
    }

}
